package com.dzf.service.impl;

import com.dzf.entity.LargeFile;
import com.dzf.entity.MyUser;
import com.dzf.entity.User;
import com.dzf.mapper.UserMapper;
import com.dzf.service.LargeFileService;

import com.dzf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    LargeFileService largeFileService;

    @Override
    public MyUser findByAccount(String account) {
        return userMapper.findByAccount(account);
    }

    @Override
    @Transactional
    public void addUser(User user,MultipartFile pic)
    {
        UUID uuid = UUID.randomUUID();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        LargeFile largeFile = new LargeFile();
        try {
            if (pic!=null&&pic.getSize()>0)
            {
                largeFile.setId(uuid.toString().replace("-",""));
                largeFile.setFilename(pic.getOriginalFilename());
                largeFile.setContent(pic.getBytes());
                largeFileService.addLargeFile(largeFile);//文件内容
            }

            if (largeFile.getId()!=null ||!" ".equals(largeFile.getId()))
            {
                user.setImg(uuid.toString().replace("-",""));
            }
            userMapper.addUser(user);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Transactional
    @Override
    public void addAdm(User user,MultipartFile pic) {
        UUID uuid = UUID.randomUUID();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        LargeFile largeFile = new LargeFile();
        try {
            if (pic!=null&&pic.getSize()>0)
            {
                largeFile.setId(uuid.toString().replace("-",""));
                largeFile.setFilename(pic.getOriginalFilename());
                largeFile.setContent(pic.getBytes());
                largeFileService.addLargeFile(largeFile);//文件内容
            }

            if (largeFile.getId()!=null ||!" ".equals(largeFile.getId()))
            {
                user.setImg(uuid.toString().replace("-",""));
            }
            userMapper.addAdm(user);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Map<String, Object> findAll(Map<String,Object> map) {
        Map<String, Object> map1 = new HashMap<>();
        map1.put("total",userMapper.findTotal());
        map1.put("users",userMapper.findAll(map));
        return map1;
    }

    @Override
    public void resetPwd(int uid) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

        Map<String,Object> map = new HashMap<>();
        map.put("uid",uid);
        map.put("password",encoder.encode("111111"));
        userMapper.resetPwd(map);
    }

    @Override
    public void close(int uid) {
        userMapper.close(uid);
    }

    @Override
    public User findById(int uid) {
        return userMapper.findById(uid);
    }


    @Override
    public int findTotal() {
        return userMapper.findTotal();
    }

    @Override
    public void updatePwd(MyUser user) {
        userMapper.updatePwd(user);
    }

    @Transactional
    @Override
    public String updateImg(MyUser user,MultipartFile img) {
        UUID uuid = UUID.randomUUID();
        LargeFile largeFile = new LargeFile();
        try {
            if (img!=null&&img.getSize()>0)
            {
                //先将图片存入数据库
                largeFile.setId(uuid.toString().replace("-",""));
                largeFile.setFilename(img.getOriginalFilename());
                largeFile.setContent(img.getBytes());
                largeFileService.addLargeFile(largeFile);//文件内容
            }

            if (largeFile.getId()!=null ||!" ".equals(largeFile.getId()))
            {
                user.setImg(uuid.toString().replace("-",""));//把图片在数据库的位置存到user中
            }
            userMapper.updateImg(user);//正式修改用户图片
        } catch (IOException e) {
            e.printStackTrace();
        }
        userMapper.updateImg(user);
        return uuid.toString();
    }

    @Override
    public void updateInfo(MyUser user) {
        userMapper.updateInfo(user);
    }


}
